package bank;

import java.util.Scanner;

public class Bank {
    private String account;//账号
    private int balance;//账户余额
    private int money;
    public Bank(String account, int balance) {
        this.account = account;
        this.balance = balance;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Bank[账号:"+account+",余额:"+balance+"]";
    }
    //存款方法
    //synchronized同步关键字,保证该方法在执行完之前不会有其他进程被执行
    public synchronized void saveAccount(){
        //获取当前账户余额
        int balance=getBalance();

        //修改余额
        System.out.println("请输入存款金额:");
        this.money();
        balance=balance+this.getMoney();
        setBalance(balance);
        //输出存款后的余额
        System.out.println("存款后的账户余额为:"+getBalance());
    }

    //取款
    public synchronized void drawAccount(){
        //获取当前余额
        int balance=getBalance();
        //修改余额
        System.out.println("请输入取款金额:");
        this.money();
        balance=balance-this.money;
        //修改账户余额
        setBalance(balance);
        System.out.println("取款后的账户余额为:"+getBalance());
    }
    public void money(){
        Scanner sc=new Scanner(System.in);
        int money=sc.nextInt();
        this.setMoney(money);
    }
}
